$(function () {
$(window).resize(function(){
         $("#jqGrid").setGridWidth($(window).width());
         $("#jqGrid").setGridHeight($(window).height());
     });
//     var height=window.screen.height;
//                    if(height<=768){
//                       height=window.screen.height*0.40;
//                     }else if(height>768 && height<=960){
//                       height=window.screen.height*0.50;
//                     }else{
//                       height=window.screen.height*0.70;
//                     }
    $("#jqGrid").jqGrid({
        url: baseURL+ 'app/getVersions/getAll',
        datatype: "json",
        colModel: [
			{ label: 'ID', name: 'id', width: 1, key: true ,hidden:true},
			{ label: '最新版本号', name: 'lastVersion',width: 100, },
			{ label: '版本日期', name: 'versionDate',hidden:true },
			{ label: '软件包大小', name: 'size', width: 60,formatter:function(cellValue){ return cellValue+"MB";} },
			{ label: '手机系统', name: 'os',width: 60, },
			{ label: '更新日志-中文', name: 'changeLogZh', width: 150 },
			{ label: '更新日志-英文', name: 'changeLogEn', width: 150 },
			{ label: '更新时间', name: 'updateTime', width: 100 },
			{ label: '强制更新', name: 'isForceUpdating', width: 50,formatter:function(cellValue){ if(cellValue==0){return "是";}else{return "否";}} },
			{ label: '使用状态', name: 'sts', width: 50,formatter:function(cellValue){ if(cellValue==0){return "在用版本";}else{return "历史版本";}} }
        ],
        ShrinkToFit:true,
		viewrecords: true,
        height: 605,
        rowNum: 10,
        rownumbers: true,
		rowList : [10,30,50],
        rownumWidth: 25,
        autowidth:true,
        gridview:true, //加速显示
        sortable:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },

        prmNames : {
            page:"page",
            rows:"limit",
            order: "order",
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	//$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        },
    });
});
var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			lastVersion:"",
			os:"",
			sts:"",
		},
		showList: true,
		title: null,
		getversions: {
		lastVersion:'',
		versionDate:'',
		},
	},
	methods: {
		query: function () {
		vm._search=true;
          			vm.reload();
          		},
		add: function(){
		$("#lastVersion").removeAttr("readonly");
			vm.showList = false;
			vm.title = "新增";
			vm.getversions = {isForceUpdating:1,sts:0,os:'android'};
		},
		clear:function(){
		 var size=$("#size").val();
         if ((/^(\d+\.\d{1,6}|[1-9][0-9]*)$/).test(size)) {
              }else{
                 	 $("#size").val("");
                 	 vm.getversions.size='';
               }
        		},
        getVersionDate:function(){
            var latestVersion=$("#lastVersion").val().trim();
            var size=$("#size").val().trim();
            var changeLogZh=$("#changeLogZh").val().trim();
            var changeLogEn=$("#changeLogEn").val().trim();
            var start=latestVersion.lastIndexOf(".")+1;
            var versionDate=latestVersion.substr(start,8);
            vm.getversions = {isForceUpdating:0,sts:0,os:'android',size:size,lastVersion:latestVersion,versionDate:versionDate,changeLogZh:changeLogZh,changeLogEn:changeLogEn};
        },
		update: function () {
		$("#lastVersion").attr({ readonly: 'true' });
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			$.get(baseURL + "app/getVersions/info/"+id, function(r){
                vm.showList = false;
                vm.title = "修改";
                vm.getversions = r.getversions;
            });
		},
		del: function (event) {
			var idsArray= getSelectedRows();
            var ids=idsArray.toString();
			if(ids == null){
				return ;
			}
			confirm('确定要把所有选中的记录改为历史版本？', function(){
				$.ajax({
					type: "get",
				    url: baseURL + "/app/getVersions/delete",
                    contentType: "application/json",
				    data: {ids:ids},
				    success: function(r){
						if(r.code == 0){
							alert('操作成功');
							vm.reload();
						}else{
							alert("操作失败");
						}
					}
				});
			});
		},
		saveOrUpdate: function () {
		if($("#versionManage").valid()){
        				var url = vm.getversions.id == null ? "app/getVersions/save" : "app/getVersions/update";
                        			$.ajax({
                        				type: "post",
                        			    url: baseURL + url,
                                        contentType: "application/json",
                        			    data: JSON.stringify(vm.getversions),
                        			    success: function(r){
                        			    	if(r.code === 0){
                        						alert('操作成功');
                        						vm.reload();
                        					}else{
                        						alert("该版本号已存在");
                        					}
                        				}
                        			});
        			}

		},
		reload: function () {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'lastVersion': vm.q.lastVersion,'os':vm.q.os,'sts':vm.q.sts,},
                page:page
            }).trigger("reloadGrid");
		}
	}
});